Resource Drawable 详解

位图

编译的资源数据类型:BitmapDrawable

位图文件

Android支持以下三种格式的位图文件:.png .jpg .gif(不建议)

在构建过程中可以通过appt工具自动优化位图文件,对图像进行无损压缩。如果计划将图像解读为比特流以转化为位图,可以放在res/raw/文件夹中,在那里不会优化

位图xml

XML 位图是在 XML 中定义的资源,指向位图文件。实际上是原始位图文件的别名。XML 可以指定位图的其他属性,例如抖动和层叠。

语法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0" encoding="utf-8"?>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
//位图资源
android:src="@[package:]drawable/drawable_resource"
//抗锯齿
android:antialias=["true" | "false"]
//位图抖动
android:dither=["true" | "false"]
//位图过滤
android:filter=["true" | "false"]
//位图的重力
android:gravity=["top" | "bottom" | "left" | "right" | "center_vertical" |
"fill_vertical" | "center_horizontal" | "fill_horizontal" |
"center" | "fill" | "clip_vertical" | "clip_horizontal"]
// mipmap 提示
android:mipMap=["true" | "false"]
//平铺模式
android:tileMode=["disabled" | "clamp" | "repeat" | "mirror"]/>

形状可绘制文件

在xml中定义的一般形状

编译的资源数据类型:GradientDrawable

语法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
//关键字shape = [矩形|椭圆|水平线|环形]
//只有在是ring时可以使用一下属性
//innerRadius 尺寸 环内部半径
//innerRadiusRatio 环内部半径 以环宽比表示
//thickness 环的厚度
//thicknessRatio 环的厚度 以环宽比表示
//useLevel 如果这用作 LevelListDrawable,则此值为“true”。这通常应为“false”,否则形状不会显示
android:shape=["rectangle" | "oval" | "line" | "ring"] >
<corners //为形状产生圆角 只有在矩形的时候适用
android:radius="integer"
android:topLeftRadius="integer"
android:topRightRadius="integer"
android:bottomLeftRadius="integer"
android:bottomRightRadius="integer" />
<gradient //指定形状渐变色
android:angle="integer" // 角度 0 为从左到右,90 为从上到上。必须是 45 的倍数。默认值为 0。
android:centerX="float" // 渐变中心的相对 X 轴位置 (0 - 1.0)。
android:centerY="float" // 渐变中心的相对 Y 轴位置 (0 - 1.0)。
android:centerColor="integer" // 颜色。起始颜色与结束颜色之间的可选颜色,以十六进制值或颜色资源表示
android:endColor="color" // 颜色。结束颜色,表示为十六进制值或颜色资源。
android:gradientRadius="integer" // 浮点型。渐变的半径。仅在 android:type="radial" 时适用。
android:startColor="color" // 颜色。起始颜色,表示为十六进制值或颜色资源
android:type=["linear" | "radial" | "sweep"] // 要应用的渐变图案的类型
android:useLevel=["true" | "false"] />
<padding // 要应用到包含视图元素的内边距(这会填充视图内容的位置,而非形状)
android:left="integer"
android:top="integer"
android:right="integer"
android:bottom="integer" />
<size // 形状的大小。
android:width="integer"
android:height="integer" />
<solid // 用于填充形状的纯色。
android:color="color" />
<stroke // 形状的笔划中线。
android:width="integer"
android:color="color"
android:dashWidth="integer" // 短划线的间距
android:dashGap="integer" // 每个短划线的大小/>
</shape>

九宫格

编译的资源数据类型:NinePatchDrawable

九宫格文件

res/drawable/myninepatch.9.png

九宫格xml

XML 九宫格是在 XML 中定义的资源,指向九宫格文件。XML 可以为图像指定抖动。

1
2
3
4
5
<?xml version="1.0" encoding="utf-8"?>
<nine-patch
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@[package:]drawable/drawable_resource"
android:dither=["true" | "false"] />

图层列表

编译的资源数据类型:LayerDrawable

1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="@[package:]drawable/drawable_resource"
android:id="@[+][package:]id/resource_name"
android:top="dimension"
android:right="dimension"
android:bottom="dimension"
android:left="dimension" />
</layer-list>

状态列表

编译的资源数据类型:StateListDrawable

StateListDrawable 是在 XML 中定义的可绘制对象,它根据对象的状态,使用多个不同的图像来表示同一个图形

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
// 。如果可绘制对象报告的内部大小在状态变更时保持不变,则值为“true”(大小是所有状态的最大值);如果大小根据当前状态而变化,则值为“false”。默认值为 false。
android:constantSize=["true" | "false"]
android:dither=["true" | "false"]
// 如果可绘制对象的内边距应根据选择的当前状态而变化,则值为“true”;如果内边距应保持不变(基于所有状态的最大内边距),则值为“false”。启用此功能要求您在状态变更时处理执行布局,这通常不受支持。默认值为 false。
android:variablePadding=["true" | "false"] >
<item
android:drawable="@[package:]drawable/drawable_resource"
android:state_pressed=["true" | "false"] // 按下对象
android:state_focused=["true" | "false"] // 对象具有输入焦点
android:state_hovered=["true" | "false"] // 光标悬停在对象上
android:state_selected=["true" | "false"] // 当前选择对象
android:state_checkable=["true" | "false"] // 对象可选中
android:state_checked=["true" | "false"] // 对象已选中
android:state_enabled=["true" | "false"] // 对象可用
android:state_activated=["true" | "false"] // 对象激活作为持续选择
android:state_window_focused=["true" | "false"] // 当前窗口有焦点/>
</selector>

级别列表

编译的资源数据类型:LevelListDrawable

管理大量备选可绘制对象的可绘制对象,每个可绘制对象都分配有最大的备选数量。使用 setLevel() 设置可绘制对象的级别值会加载级别列表中 android:maxLevel 值大于或等于传递到方法的值的可绘制对象资源。

1
2
3
4
5
6
7
8
<?xml version="1.0" encoding="utf-8"?>
<level-list
xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="@drawable/drawable_resource"
android:maxLevel="integer"
android:minLevel="integer" />
</level-list>

转换可绘制文件

编译的资源数据类型:TransitionDrawable

可在两种可绘制对象资源之间交错淡出的可绘制对象。每个可绘制对象由单一 元素内的 元素表示。不支持超过两个项目。要向前转换,请调用 startTransition()。要向后转换,则调用 reverseTransition()。

1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0" encoding="utf-8"?>
<transition
xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="@[package:]drawable/drawable_resource"
android:id="@[+][package:]id/resource_name"
android:top="dimension"
android:right="dimension"
android:bottom="dimension"
android:left="dimension" />
</transition>

插入可绘制文件

编译的资源数据类型:InsetDrawable

在 XML 文件中定义的以指定距离插入其他可绘制对象的可绘制对象。当视图需要小于视图实际边界的背景时,此类可绘制对象很有用。

1
2
3
4
5
6
7
8
<?xml version="1.0" encoding="utf-8"?>
<inset
xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/drawable_resource"
android:insetTop="dimension"
android:insetRight="dimension"
android:insetBottom="dimension"
android:insetLeft="dimension" />

裁剪可绘制文件

编译的资源数据类型:ClipDrawable

在 XML 文件中定义的对其他可绘制对象进行裁剪(根据其当前级别)的可绘制对象。您可以根据级别以及用于控制其在整个容器中位置的重力,来控制子可绘制对象的裁剪宽度和高度。通常用于实现进度栏之类的项目。

1
2
3
4
5
6
7
8
<?xml version="1.0" encoding="utf-8"?>
<clip
xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/drawable_resource"
android:clipOrientation=["horizontal" | "vertical"] // 裁剪方向
android:gravity=["top" | "bottom" | "left" | "right" | "center_vertical" |
"fill_vertical" | "center_horizontal" | "fill_horizontal" |
"center" | "fill" | "clip_vertical" | "clip_horizontal"] />

缩放可绘制文件

编译的资源数据类型:ScaleDrawable

在 XML 文件中定义的更改其他可绘制对象大小(根据其当前级别)的可绘制对象。

1
2
3
4
5
6
7
8
9
10
<?xml version="1.0" encoding="utf-8"?>
<scale
xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/drawable_resource"
//缩放后重心位置
android:scaleGravity=["top" | "bottom" | "left" | "right" | "center_vertical" |
"fill_vertical" | "center_horizontal" | "fill_horizontal" |
"center" | "fill" | "clip_vertical" | "clip_horizontal"]
android:scaleHeight="percentage" // 缩放高度百分比
android:scaleWidth="percentage" // 缩放宽度百分比/>

动画可绘制文件

编译的资源数据类型:AnimationDrawable

1
2
3
4
5
6
7
8
<animation-list android:id="@+id/selected" android:oneshot="false">
<item android:drawable="@drawable/wheel0" android:duration="50" />
<item android:drawable="@drawable/wheel1" android:duration="50" />
<item android:drawable="@drawable/wheel2" android:duration="50" />
<item android:drawable="@drawable/wheel3" android:duration="50" />
<item android:drawable="@drawable/wheel4" android:duration="50" />
<item android:drawable="@drawable/wheel5" android:duration="50" />
</animation-list>